package com.mender.trade.controller.admin;

import com.mender.common.result.PageResult;
import com.mender.common.result.Result;
import com.mender.domain.dto.GoodsSalesDTO;
import com.mender.domain.po.Orders;
import com.mender.trade.domain.dto.OrdersCancelDTO;
import com.mender.trade.domain.dto.OrdersConfirmDTO;
import com.mender.trade.domain.dto.OrdersPageQueryDTO;
import com.mender.trade.domain.dto.OrdersRejectionDTO;
import com.mender.trade.domain.vo.OrderStatisticsVO;
import com.mender.trade.domain.vo.OrderVO;
import com.mender.trade.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "商家端订单管理模块")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageResult pageResult = orderService.pageQuery(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数统计")
    public Result<OrderStatisticsVO> statistics() {
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }

    @GetMapping("/details/{id}")
    @ApiOperation("查看订单详情")
    public Result<OrderVO> details(@PathVariable Long id) {
        log.info("查看订单详情：{}", id);
        OrderVO orderVO = orderService.details(id);
        return Result.success(orderVO);
    }

    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("接单：{}", ordersConfirmDTO.getId());
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result refuse(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        log.info("拒单：{}", ordersRejectionDTO);
        orderService.refuse(ordersRejectionDTO);
        return Result.success();
    }

    @PutMapping("/cancel")
    @ApiOperation("商家取消订单")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        log.info("商家取消订单：{}", ordersCancelDTO);
        orderService.adminCancel(ordersCancelDTO);
        return Result.success();
    }

    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result delivery(@PathVariable Long id) {
        log.info("派送订单：{}", id);
        orderService.delivery(id);
        return Result.success();
    }

    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result complete(@PathVariable Long id) {
        log.info("完成订单：{}", id);
        orderService.complete(id);
        return Result.success();
    }

    /**
     * 查询订单总数
     */
    @PostMapping("/countByMap")
    @ApiOperation("查询订单总数")
    public Result<Integer> countOrderByMap(@RequestBody Map<String, Object> params) {
        Integer count = orderService.countOrderByMap(params);
        return Result.success(count);
    }

    /**
     * 营业额统计
     */
    @PostMapping("/sumByMap")
    @ApiOperation("营业额统计")
    public Result<Double> sumByMap(@RequestBody Map<String, Object> params) {
        Double sum = orderService.sumByMap(params);
        return Result.success(sum);
    }

    /**
     * 查询所有已完成订单数据（一次性查出范围内所有订单）
     * getCompletedOrdersByTime
     */
    @GetMapping("/getCompletedOrders")
    @ApiOperation("查询所有已完成订单数据")
    public Result<List<Orders>> getCompletedOrdersByTime(
            @RequestParam("beginTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
            @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime endTime,
            @RequestParam(required = false) Integer status) {
        List<Orders> orders = orderService.getCompletedOrdersByTime(beginTime, endTime,status);
        return Result.success(orders);
    }

    /**
     * 根据指定时间查询符合条件的菜品
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/getOrderDetailTop10")
    @ApiOperation("根据指定时间查询符合条件的菜品")
    public Result<List<GoodsSalesDTO>> getOrderDetailTop10(
            @RequestParam("beginTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
            @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime endTime) {
        List<GoodsSalesDTO> ordersDetails = orderService.getOrderDetailTop10(beginTime, endTime);
        return Result.success(ordersDetails);
    }
}
